﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using MSAcademic.v2.Infrastructure.Picture;
using MSAcademic.v2.Infrastructure.Utility;
using MSAcademic.v2.Web.Areas.Admin.Models.Picture;
using MSAcademic.v2.Web.Areas.Admin.Models.User;

namespace MSAcademic.v2.Web.Areas.Admin.Controllers
{
    public partial class PictureController
    {
        [HttpGet]
        public ActionResult UploadFrontPictures(int articleId)
        {
            var viewModel = _uploadFrontPicturesViewModel();
            viewModel.ArticleId = articleId;

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult UploadFrontPictures(int articleId, HttpPostedFileBase frontPicture)
        {
            if(!frontPicture.ContentType.Contains("image"))
            {
                return Redirect(Request.UrlReferrer.ToString());
            }

            _pictureService.ArticleFrontPictureUpload(articleId, frontPicture);

            return RedirectToRoute("UploadArticleFrontPictures", new { id = articleId });
        }

        [HttpGet]
        public ActionResult ShowArticleFrontPictures(int articleId)
        {
            var viewModel = _showArticleFrontPicturesViewModelFactory();
            viewModel.ArticleId = articleId;

            return PartialView(viewModel);
        }
    }

    public partial class PictureController
    {
        [HttpGet]
        public ActionResult EditArticleThumbnailPicture(int articleId)
        {
            var viewModel = _editArticleThumbnailViewModelFactory();

            viewModel.ArticleId = articleId;
            viewModel.OriginalUrl=viewModel.ThumbnailPictureUrl;

            return View(viewModel);
        }
        [HttpPost]
        public ActionResult EditArticleThumbnailPicture(EditArticleThumbnailPictureViewModel viewModel)
        {
            var path = _pictureService.BuildCropedPicture(viewModel.CroppedThumbnailPictureUrl, viewModel.OriginalUrl,"cropedthumbnail");
            
            _pictureCommands.SetArticleThumbnailPictureUrl(viewModel.ArticleId, path);

            return RedirectToRoute("UploadArticleFrontPictures", new { id = viewModel.ArticleId });
        }

        [HttpGet] 
        public ActionResult EditArticleNormalPicture(int articleId)
        {
            var viewModel = _editArticleNormalPictureViewModelFactory();

            viewModel.ArticleId = articleId;
            viewModel.OriginalUrl = viewModel.NormalPictureUrl;

            return View(viewModel);
        }
        [HttpPost]
        public ActionResult EditArticleNormalPicture(EditArticleNormalPictureViewModel viewModel)
        {
            var path = _pictureService.BuildCropedPicture(viewModel.CroppedNormalPictureUrl, viewModel.OriginalUrl,"cropednormal");

            _pictureCommands.SetArticleNormalPictureUrl(viewModel.ArticleId, path);

            return RedirectToRoute("UploadArticleFrontPictures", new { id = viewModel.ArticleId });
        }
        [HttpGet]
        public ActionResult EditArticleBigPicture(int articleId)
        {
            var viewModel = _editArticleBigPictureViewModelFactory();

            viewModel.ArticleId = articleId;
            viewModel.OriginalUrl = viewModel.BigPictureUrl;

            return View(viewModel);
        }
        [HttpPost]
        public ActionResult EditArticleBigPicture(EditArticleBigPictureViewModel viewModel)
        {
            var path = _pictureService.BuildCropedPicture(viewModel.CroppedBigPictureUrl, viewModel.OriginalUrl,"cropedbig");

            _pictureCommands.SetArticleBigPictureUrl(viewModel.ArticleId, path);

            return RedirectToRoute("UploadArticleFrontPictures", new { id = viewModel.ArticleId });
        }
        
    }
    public partial class PictureController
    {
        [HttpGet]
        public ActionResult EditGalleryPicture(int pictureId)
        {
            var viewModel = _editPictureGalleryViewModelFactory();

            viewModel.PictureId = pictureId;
            viewModel.PictureIsHidden = viewModel.Picture.IsHidden;
            viewModel.GalleryId = viewModel.Picture.GalleryId;
            viewModel.PictureCaption = viewModel.Picture.Caption;
            viewModel.OriginalPictureUrl = viewModel.OriginalUrl;

            return View(viewModel);
        }

        [HttpPost]
        public ActionResult EditGalleryPicture(EditGalleryPictureViewModel viewModel)
        {
            _pictureCommands.EditPicture(viewModel.PictureId, viewModel.PictureCaption, viewModel.PictureIsHidden);

            return RedirectToRoute("ShowGallery", new { galleryId = viewModel.GalleryId });
        }
        
    }
    public partial  class PictureController
    {
        [HttpGet]
        public ActionResult ChangeProfilePicture()
        {
            var viewModel = _userProfilePictureViewModelFactory();
            if (viewModel.SignedInUser == null)
            {
                return RedirectToRoute("Home");
            }
            
            ViewData["CurrentView"] = "ProfilePicture";
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult DownloadAndSave(ChangeProfilePictureViewModel viewModel)
        {
            ViewData["CurrentView"] = "ProfilePicture";

            if (!ModelState.IsValid)
            {
                viewModel.ErrorMessage = "Niste popunili URL slike za upload, ili je nešto pošlo jako po zlu!";
                return View("ChangeProfilePicture", viewModel);
            }

            if (!_urlValidator.IsUrlValid(viewModel.Url))
            {
                viewModel.ErrorMessage = ConfigurationManager.AppSettings["BadUrlInputForPictureDownload"];
                return View("ChangeProfilePicture", viewModel);
            }
             
            var path = _pictureService.DownloadAndUploadUserProfilePicture(viewModel.Url, viewModel.SignedInUser.Id);
            if (path != "-1")
            {
                _pictureCommands.SetUserProfilePicture(viewModel.SignedInUser.Id, path);
                return RedirectToRoute("CropProfilePicture");
            }
            
            return View("ChangeProfilePicture", viewModel);
        }

        [HttpPost]
        public ActionResult Upload(ChangeProfilePictureViewModel viewModel, HttpPostedFileBase picture)
        {
            ViewData["CurrentView"] = "ProfilePicture";

            if (picture == null) {
                viewModel.ErrorMessage = "Niste odabrali sliku za upload!";
                return View("ChangeProfilePicture", viewModel);
            } 
            _pictureService.UserProfilePictureUpload(viewModel.SignedInUser.Id, picture);
            
            return RedirectToRoute("CropProfilePicture");
        }
        [HttpGet]
        public ActionResult CropProfilePicture()
        {
            var viewModel = _cropUserProfilePicViewModelFactory();
            viewModel.OriginalUrl = viewModel.PictureUrl;
            
            return View(viewModel);
        }
        [HttpPost]
        public ActionResult CropProfilePicture(EditUserProfilePictureViewModel viewModel)
        {
            var path = _pictureService.BuildCropedPicture(viewModel.CropedPictureUrl, viewModel.PictureUrl, "profilecroped");
            _pictureCommands.SetUserProfilePicture(viewModel.SignedInUser.Id,path);

            return RedirectToRoute("ChangeProfilePicture");
        }
    }
    public partial class PictureController : Controller
    {
        private readonly IPictureService _pictureService;
        private readonly IPictureCommands _pictureCommands;
        private readonly Text _urlValidator;
        private readonly Func<UploadFrontPicturesViewModel> _uploadFrontPicturesViewModel;
        private readonly Func<EditGalleryPictureViewModel> _editPictureGalleryViewModelFactory;
        private readonly Func<ShowArticleFrontPicturesViewModel> _showArticleFrontPicturesViewModelFactory;
        private readonly Func<EditArticleThumbnailPictureViewModel> _editArticleThumbnailViewModelFactory;
        private readonly Func<EditArticleNormalPictureViewModel> _editArticleNormalPictureViewModelFactory;
        private readonly Func<EditArticleBigPictureViewModel> _editArticleBigPictureViewModelFactory;
        private readonly Func<ChangeProfilePictureViewModel> _userProfilePictureViewModelFactory;
        private readonly Func<EditUserProfilePictureViewModel> _cropUserProfilePicViewModelFactory;

        public PictureController(Func<EditArticleBigPictureViewModel> editArticleBigPictureViewModelFactory, 
            Func<EditArticleNormalPictureViewModel> editArticleNormalPictureViewModelFactory, 
            Func<EditArticleThumbnailPictureViewModel> editArticleThumbnailViewModelFactory,
            Func<UploadFrontPicturesViewModel> uploadFrontPicturesViewModel,
            Func<ShowArticleFrontPicturesViewModel> showArticleFrontPicturesViewModelFactory, 
            Func<EditGalleryPictureViewModel> editPictureViewModel, IPictureService fileSystemPictureService,
            Func<ChangeProfilePictureViewModel> userProfilePictureViewModelFactory,
            Func<EditUserProfilePictureViewModel> cropUserProfilePicViewModelFactory,
            IPictureCommands pictureCommands,
            Text urlValidator)
        {
            _cropUserProfilePicViewModelFactory = cropUserProfilePicViewModelFactory;
            _userProfilePictureViewModelFactory = userProfilePictureViewModelFactory;
            _pictureService = fileSystemPictureService;
            _pictureCommands = pictureCommands;
            _uploadFrontPicturesViewModel = uploadFrontPicturesViewModel;
            _editPictureGalleryViewModelFactory = editPictureViewModel;
            _showArticleFrontPicturesViewModelFactory = showArticleFrontPicturesViewModelFactory;
            _editArticleThumbnailViewModelFactory = editArticleThumbnailViewModelFactory;
            _editArticleNormalPictureViewModelFactory = editArticleNormalPictureViewModelFactory;
            _editArticleBigPictureViewModelFactory = editArticleBigPictureViewModelFactory;
            _urlValidator = urlValidator;
        }
    }
}
